package com.chengang.crawler.jd.task;

import com.chengang.crawler.jd.pojo.Item;
import com.chengang.crawler.jd.service.ItemService;
import com.chengang.crawler.jd.util.HttpUtils;
import org.apache.http.client.utils.HttpClientUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 陈刚
 * @ClassName ItemTask
 * @Description //TODO
 * @create 2020-02-09 11:00
 */
@Component
public class ItemTask {
    @Autowired
    HttpUtils httpUtils;

    @Autowired
    private ItemService itemService;

    @Scheduled(fixedDelay = 1*1000)
    public void itemTask() {
        String url = "https://search.jd.com/Search?keyword=手机&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=手机&s=55&click=0&page=";
        for (int i = 1; i < 10; i += 2) {
            String html = httpUtils.doGetHtml(url + i);

            this.parse(html);
        }
    }

    /**
     * 解析页面获取商品数据并存储
     * @param html
     */
    private void parse(String html) {
        //解析html获取dom对象
        Document document = Jsoup.parse(html);
        Elements elements = document.select("#J_goodsList>ul>li");
        elements.stream().forEach(
            element -> {
                long spu = Long.parseLong(element.attr("data-spu"));
                Elements skus = element.select("li.ps-item");
                for (Element skuEle : skus) {

                    long sku = Long.parseLong(skuEle.select("[data-sku]").attr("data-sku"));
                    //根据sku获取商品数据
                    Item item = new Item();
                    item.setSku(sku);
                    List<Item> itemList = itemService.findAll(item);
                    if (!CollectionUtils.isEmpty(itemList)) {
                        //如果商品存在就不保存，因为已经存在
                        continue;
                    }
                    item.setSpu(spu);
                    String itemUrl = "https://item.jd.com/"+sku+".html";
                    item.setUrl(itemUrl);
                    //获取商品图片
                    String picUrl = skuEle.select("img[data-sku]").attr("data-lazy-img");
                    picUrl = "https:"+picUrl.replace("/n9/", "/n1/");
                    String picName = this.httpUtils.doGetImage(picUrl);
                    item.setPic(picName);
//                    //获取商品价格
//                    item.setPrice();
//                    //获取商品标题
//                    item.setTitle();
                    item.setCreated(new Date());
                    item.setUpdated(item.getCreated());
                }
            }
        );
    }
}
